package com.qbw.app.module.home.holder;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.GridLayoutManager;

import com.http.okhttp.HttpTask;
import com.qbw.app.R;
import com.qbw.app.databinding.HomeHolderStatussBinding;
import com.qbw.app.manager.UnitManager;
import com.qbw.app.model.Coolant;
import com.qbw.app.model.ProblemStatus;
import com.qbw.app.model.TireData;
import com.qbw.app.model.Vehicle;
import com.qbw.app.model.VehicleHomeInfo;
import com.qbw.app.model.Voltage;
import com.qbw.app.module.common.holder.CommonHolder;
import com.qbw.app.module.home.adapter.HomeAdapter;
import com.qbw.app.module.home.adapter.HomeViewType;
import com.qbw.app.module.vehicle.VehicleDataBindingAdapter;
import com.qbw.app.module.vehicle.activity.VehicleEstimateDistanceActivity;
import com.qbw.core.model.ViewType;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xiansx
 * @date 2020/10/12
 */
public class HomeStatussHolder extends CommonHolder<HomeStatussHolder.Data, HomeHolderStatussBinding> {

    private HomeAdapter mStatusAdapter;
    private HttpTask mVoltageTask;
    private HttpTask mCoolantTask;
    private HttpTask mInfoTask;
    private List<Integer> mStatusChildSortedViewTypes = new ArrayList<>();

    public HomeStatussHolder(Context context, ViewGroup viewParent) {
        super(context, R.layout.home_holder_statuss, viewParent);
        GridLayoutManager gmStatus = new GridLayoutManager(context, 3);
        mBinding.rcvStatus.setLayoutManager(gmStatus);
        mBinding.rcvStatus.setItemAnimator(null);
        mStatusAdapter = new HomeAdapter(context, mListener);
        mBinding.rcvStatus.setAdapter(mStatusAdapter);
        mStatusChildSortedViewTypes.add(HomeViewType.VT_STATUS_ITEM_BATTERY);
        mStatusChildSortedViewTypes.add(HomeViewType.VT_STATUS_ITEM_COOLANT);
        mStatusChildSortedViewTypes.add(HomeViewType.VT_STATUS_ITEM_PROBLEM);
        mStatusChildSortedViewTypes.add(HomeViewType.VT_STATUS_ITEM_TIRE);
    }

    @Override
    public void bindData(int position, Data data) {
        super.bindData(position, data);
        updateUI();
        mBinding.tvKm.setText(UnitManager.getInstance().isUseKm() ? "km" : "mile");
        updateResidualFuelUi(mData.getHomeInfo().getResidualFuel());
        updateMileagelUi(mData.getHomeInfo().getMileage());
        updateVisible(mData.getHomeInfo().getVehicle());
        itemView.setOnClickListener(v -> {
            if (mBinding.clMilleage.getVisibility() != View.VISIBLE) {
                return;
            }
            Vehicle vehicle = mData.getHomeInfo().getVehicle();
            if (vehicle != null) {
                VehicleEstimateDistanceActivity.start(mContext, vehicle);
            }
        });


    }


    /**
     * 电池电量
     */
    private void updateResidualFuelUi(VehicleHomeInfo.ResidualFuel fuel) {
        mBinding.tvNumber.setTextColor(mContext.getResources().getColor(R.color.nm_normal));
        mBinding.tvPercent.setTextColor(mContext.getResources().getColor(R.color.nm_normal));

        int ipercent = fuel == null ? 0 : (int) fuel.getValue();
        if (fuel == null || (fuel.getState() + "").equals(Voltage.NO_DATA)) {
            mBinding.tvNumber.setText(R.string.na);
            mBinding.ivBattery.setImageResource(R.drawable.nm_icon_battery_disable);
        } else {
            if(ipercent > 100) {
                ipercent = 100;
            }
            mBinding.tvNumber.setText(String.valueOf(ipercent));
            if(ipercent >= 10) {
                mBinding.ivBattery.setImageResource(R.drawable.nm_icon_battery_normal);
            } else {
                mBinding.tvNumber.setTextColor(mContext.getResources().getColor(R.color.nm_exception));
                mBinding.tvPercent.setTextColor(mContext.getResources().getColor(R.color.nm_exception));
                mBinding.ivBattery.setImageResource(R.drawable.nm_icon_battery_low);
            }
        }
        mBinding.pbBattery.setProgress(ipercent);
        mBinding.pbMilleage.setProgress(ipercent);
    }

    /**
     * 预估里程
     */
    private void updateMileagelUi(VehicleHomeInfo.Mileage mileage) {
        if (mileage == null || (mileage.getState() + "").equals(Voltage.NO_DATA)) {
            mBinding.tvNumberMilleage.setText(R.string.na);
        } else {
            VehicleDataBindingAdapter.setDistanceValue(mBinding.tvNumberMilleage,
                    mileage.getValue());
        }
    }

    private void updateVisible(Vehicle vehicle) {
        //是否显示剩余油量，如果剩余油量不显示，那么预估里程默认也不显示
        boolean showOilRemainder = vehicle != null && vehicle.hasMenuId(Vehicle.MENU_ID_OIL_REMAINDER);
        //是否显示预估里程
        boolean showLicheng = vehicle != null && vehicle.hasMenuId(Vehicle.MENU_ID_ESTIMATEDMILEAGE);
        if (!showOilRemainder) {
            showLicheng = false;
        }
        mBinding.clBattery.setVisibility(showOilRemainder ? View.VISIBLE : View.GONE);
        mBinding.clMilleage.setVisibility(showLicheng ? View.VISIBLE : View.GONE);
        if(!showOilRemainder){
            mBinding.clBatteryMilleage.setVisibility(View.GONE);
            mBinding.viewLine.setVisibility(View.GONE);
        }else{
            mBinding.clBatteryMilleage.setVisibility(View.VISIBLE);
            mBinding.viewLine.setVisibility(View.VISIBLE);
        }
        if(!showLicheng){
            mBinding.viewCentreLine.setVisibility(View.GONE);
        }else{
            mBinding.viewCentreLine.setVisibility(View.VISIBLE);
        }
    }

    public static boolean canShow(Vehicle vehicle) {
        //是否显示剩余油量，如果剩余油量不显示，那么预估里程默认也不显示
        boolean showOilRemainder = vehicle.hasMenuId(Vehicle.MENU_ID_OIL_REMAINDER);
        //是否显示预估里程
        boolean showLicheng = vehicle.hasMenuId(Vehicle.MENU_ID_ESTIMATEDMILEAGE);
        if (!showOilRemainder) {
            showLicheng = false;
        }
        return showOilRemainder || showLicheng;
    }



    private void checkNoStatusData() {
        if (mStatusAdapter.getChildCount() <= 0) {
            mBinding.rcvStatus.setVisibility(View.GONE);
        } else {
            mBinding.rcvStatus.setVisibility(View.VISIBLE);
            GridLayoutManager gmStatus = new GridLayoutManager(mContext,
                                                               mStatusAdapter.getChildCount());
            mBinding.rcvStatus.setLayoutManager(gmStatus);
        }
    }

    public void updateUI() {
        Vehicle vehicle = mData.getHomeInfo().getVehicle();
        Voltage voltage = mData.getHomeInfo().getVoltage();
        voltage.setVehicle(mData.getHomeInfo().getVehicle());
        int p = mStatusAdapter.getChildPositionByViewType(HomeViewType.VT_STATUS_ITEM_BATTERY);
        ViewType viewType = new ViewType(HomeViewType.VT_STATUS_ITEM_BATTERY, voltage);
        //是否显示电瓶电压
        boolean showVoltage = vehicle != null && vehicle.hasMenuId(Vehicle.MENU_ID_BATTERYVOLTAGE);
        if (showVoltage) {
            if (p == -1) {
                mStatusAdapter.addChild(mStatusAdapter.getChildPosition(HomeViewType.VT_STATUS_ITEM_BATTERY,
                        mStatusChildSortedViewTypes),
                        viewType);
            } else {
                mStatusAdapter.updateChild(p, viewType);
            }
        } else {
            mStatusAdapter.removeChild(p);
        }

        Coolant coolant = mData.getHomeInfo().getCoolantTemperature();
        coolant.setVehicle(mData.getHomeInfo().getVehicle());
        p = mStatusAdapter.getChildPositionByViewType(HomeViewType.VT_STATUS_ITEM_COOLANT);
        viewType = new ViewType(HomeViewType.VT_STATUS_ITEM_COOLANT, coolant);
        //是否显示发动机温度
        boolean showCoolant = vehicle != null && vehicle.hasMenuId(Vehicle.MENU_ID_COOLANTTEMPERATURE);
        if (showCoolant) {
            if (p == -1) {
                mStatusAdapter.addChild(mStatusAdapter.getChildPosition(HomeViewType.VT_STATUS_ITEM_COOLANT,
                        mStatusChildSortedViewTypes),
                        viewType);
            } else {
                mStatusAdapter.updateChild(p, viewType);
            }
        } else {
            mStatusAdapter.removeChild(p);
        }

        ProblemStatus problem = mData.getHomeInfo().getFault();
        problem.setVehicle(mData.getHomeInfo().getVehicle());
        p = mStatusAdapter.getChildPositionByViewType(HomeViewType.VT_STATUS_ITEM_PROBLEM);
        viewType = new ViewType(HomeViewType.VT_STATUS_ITEM_PROBLEM, problem);
        //是否显示故障
        boolean showFault = vehicle != null && vehicle.hasMenuId(Vehicle.MENU_ID_VEHICLEBREAKDOWN);
        if(showFault) {
            if (p == -1) {
                mStatusAdapter.addChild(mStatusAdapter.getChildPosition(HomeViewType.VT_STATUS_ITEM_PROBLEM,
                        mStatusChildSortedViewTypes),
                        viewType);
            } else {
                mStatusAdapter.updateChild(p, viewType);
            }
        } else {
            mStatusAdapter.removeChild(p);
        }

        TireData tireData = mData.getHomeInfo().getTirePressure();
        tireData.setVehicle(mData.getHomeInfo().getVehicle());
        p = mStatusAdapter.getChildPositionByViewType(HomeViewType.VT_STATUS_ITEM_TIRE);
        viewType = new ViewType(HomeViewType.VT_STATUS_ITEM_TIRE, tireData);
        //是否显示tire
        boolean showTire = vehicle != null && vehicle.hasMenuId(Vehicle.MENU_ID_TIRE);
        if(showTire) {
            if (p == -1) {
                mStatusAdapter.addChild(mStatusAdapter.getChildPosition(HomeViewType.VT_STATUS_ITEM_TIRE,
                        mStatusChildSortedViewTypes),
                        viewType);
            } else {
                mStatusAdapter.updateChild(p, viewType);
            }
        } else {
            mStatusAdapter.removeChild(p);
        }

        checkNoStatusData();
    }

    public static boolean isCanShow(Vehicle vehicle) {
        return vehicle.hasMenuId(Vehicle.MENU_ID_BATTERYVOLTAGE) || vehicle.hasMenuId(Vehicle.MENU_ID_COOLANTTEMPERATURE) || vehicle
                .hasMenuId(Vehicle.MENU_ID_VEHICLEBREAKDOWN) || vehicle.hasMenuId(Vehicle.MENU_ID_TIRE);
    }

    public static class Data {
        private VehicleHomeInfo homeInfo;

        public Data(VehicleHomeInfo homeInfo) {
            this.homeInfo = homeInfo;
        }

        public VehicleHomeInfo getHomeInfo() {
            return homeInfo;
        }

        public void setHomeInfo(VehicleHomeInfo homeInfo) {
            this.homeInfo = homeInfo;
        }
    }

}
